import qs from 'qs'

/**
 * url 连接新 参数
 */
function urlConcat (url, key, val) {
  let splitChar = '?'
  if (url.indexOf('?') !== -1) {
    splitChar = '&'
  }
  return url + splitChar + key + '=' + encodeURIComponent(val)
}
/**
 * 读取请求地址的参数
 */
function getURLParams () {
  let URL = location.href
  let urlSplit = URL.split('?')
  if (urlSplit[1]) {
    return qs.parse(urlSplit[1])
  }
  return {}
}

/**
 * 拆解url 返回 url params
 */
function fitUrl (URL) {
  let urlSplit = URL.split('?')
  let params = {}
  if (urlSplit[1]) {
    params = qs.parse(urlSplit[1])
  }
  return { url: urlSplit[0], params: params }
}

/**
 * url 添加 token
 */
function urlMakeToken (url, token, key = 'token') {
  if (token) {
    return urlConcat(url, key, token)
  }
  return url
}

export default {
  urlConcat,
  getURLParams,
  urlMakeToken,
  fitUrl
}
